package com.ruoyi.training.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import com.ruoyi.training.dto.TrainingClassBatchDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.TrainingSchedule;
import com.ruoyi.training.service.ITrainingScheduleService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 培训计划明细（单节课程/活动）Controller
 *
 * @author WorrilessGo
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/TrainingSchedule")
public class TrainingScheduleController extends BaseController
{
    @Autowired
    private ITrainingScheduleService trainingScheduleService;

    /**
     * 查询培训计划明细（单节课程/活动）列表
     */
    @RequiresPermissions("TrainingSchedule:TrainingSchedule:list")
    @GetMapping("/list")
    public TableDataInfo list(TrainingSchedule trainingSchedule)
    {
        startPage();
        List<TrainingSchedule> list = trainingScheduleService.selectTrainingScheduleList(trainingSchedule);
        return getDataTable(list);
    }

    /**
     * 通过班级ID查询培训计划列表
     * lqx
     */
//    @RequiresPermissions("TrainingSchedule:TrainingSchedule:list")
    @GetMapping("/listByClassId")
    public TableDataInfo listByClassId(@NotBlank(message = "班级ID不能为空") String trainingClassId) {
        List<TrainingSchedule> list = trainingScheduleService.selectByClassId(trainingClassId);
        return getDataTable(list);
    }

    /**
     * 导出培训计划明细（单节课程/活动）列表
     */
    @RequiresPermissions("TrainingSchedule:TrainingSchedule:export")
    @Log(title = "培训计划明细（单节课程/活动）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainingSchedule trainingSchedule)
    {
        List<TrainingSchedule> list = trainingScheduleService.selectTrainingScheduleList(trainingSchedule);
        ExcelUtil<TrainingSchedule> util = new ExcelUtil<TrainingSchedule>(TrainingSchedule.class);
        util.exportExcel(response, list, "培训计划明细（单节课程/活动）数据");
    }

    /**
     * 获取培训计划明细（单节课程/活动）详细信息
     */
    @RequiresPermissions("TrainingSchedule:TrainingSchedule:query")
    @GetMapping(value = "/{trainingScheduleId}")
    public AjaxResult getInfo(@PathVariable("trainingScheduleId") String trainingScheduleId)
    {
        return success(trainingScheduleService.selectTrainingScheduleByTrainingScheduleId(trainingScheduleId));
    }

    /**
     * 新增培训计划明细（单节课程/活动）
     */
//    @RequiresPermissions("TrainingSchedule:TrainingSchedule:add")
    @Log(title = "培训计划明细（单节课程/活动）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainingSchedule trainingSchedule)
    {
        return toAjax(trainingScheduleService.insertTrainingSchedule(trainingSchedule));
    }

    /**
     * 批量新增培训计划明细（单节课程/活动）
     * lqx
     */
//    @RequiresPermissions("TrainingSchedule:TrainingSchedule:add")
    @Log(title = "培训计划明细", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestBody List<TrainingSchedule> trainingSchedules)
    {
        return toAjax(trainingScheduleService.batchInsertTrainingSchedule(trainingSchedules));
    }

    /**
     * 批量更新班级的培训计划（先删除该班级所有计划，再新增传递的列表）
     * lqx
     */
//    @RequiresPermissions("TrainingSchedule:TrainingSchedule:edit")
    @Log(title = "培训计划明细", businessType = BusinessType.UPDATE)
    @PostMapping("/batch/class")
    public AjaxResult batchUpdateByClass(@Valid @RequestBody TrainingClassBatchDTO batchDTO) {
        return toAjax(trainingScheduleService.batchUpdateByClass(batchDTO));
    }

    /**
     * 修改培训计划明细（单节课程/活动）
     */
//    @RequiresPermissions("TrainingSchedule:TrainingSchedule:edit")
    @Log(title = "培训计划明细（单节课程/活动）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainingSchedule trainingSchedule)
    {
        return toAjax(trainingScheduleService.updateTrainingSchedule(trainingSchedule));
    }

    /**
     * 删除培训计划明细（单节课程/活动）
     */
    @RequiresPermissions("TrainingSchedule:TrainingSchedule:remove")
    @Log(title = "培训计划明细（单节课程/活动）", businessType = BusinessType.DELETE)
    @DeleteMapping("/{trainingScheduleIds}")
    public AjaxResult remove(@PathVariable String[] trainingScheduleIds)
    {
        return toAjax(trainingScheduleService.deleteTrainingScheduleByTrainingScheduleIds(trainingScheduleIds));
    }

    /**
     * 删除培训计划明细（单节课程/活动）
     * lqx
     */
    @DeleteMapping("/deleteTrainingSchedule/{trainingScheduleId}")
    public AjaxResult deleteTrainingScheduleById(@PathVariable("trainingScheduleId") String trainingScheduleId)
    {
        int result = trainingScheduleService.deleteTrainingScheduleByTrainingScheduleId(trainingScheduleId);
        if (result == 100){
            return AjaxResult.error("该培训计划被关联签到，不能删除");
        }
        return toAjax(result);
    }
}
